package com.jackstraw.jack_base.remote_command.entity;

import com.jackstraw.jack_base.remote_command.config.JschConfig;
import com.jackstraw.jack_base.remote_command.pool.entity.SessiontConnectionConfig;
import lombok.Data;
import org.apache.commons.lang3.ObjectUtils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static com.jackstraw.jack_base.remote_command.entity.EnvProperties.*;

/**
 * 系统初始化时需要传入的配置属性
 */
@Data
public class InitVo {
    /**
     * 连接名称
     */
    private String connName;
    /**
     * 初始化模式（单机or集群）
     */
    private ModelEnum modelEnum = ModelEnum.single;

    private Map<String, String> dictMap = new LinkedHashMap<>();

    private Map<String, Map<String, String>> envPropertiesMap = new LinkedHashMap<>();

    private Map<String, List<String>> dictConnMap = new LinkedHashMap<>();

    private Boolean flag = Boolean.FALSE;

    public Map<String, List<String>> resolveEnv(JschConfig jschConfig) {
        if (this.getFlag()) {
            return this.dictConnMap;
        }
        Map<String, SessiontConnectionConfig> sessiontConnectionConfigMap = jschConfig.getSessiontConnectionConfigMap();
        Map<String, Map<String, String>> jackDictPropertiesMap = jschConfig.getJackDictPropertiesMap();
        if (ModelEnum.single.equals(this.getModelEnum())) {
            if (!envPropertiesMap.containsKey(connName)) {
                envPropertiesMap.put(connName, new LinkedHashMap<>());
            }
        }
        dictMap.forEach((key, value) -> {
            envPropertiesMap.keySet().stream().forEach(connName -> {
                if (!envPropertiesMap.get(connName).containsKey(key)) {
                    envPropertiesMap.get(connName).put(key, value);
                }
            });
        });
        envPropertiesMap.forEach((connName, envMap) -> {
            List<String> envList = new ArrayList<>();

            Map<String, String> dictPropertiesMap = jackDictPropertiesMap.get(connName);
            if (ObjectUtils.isNotEmpty(dictPropertiesMap)) {
                dictPropertiesMap.forEach((key, value) -> {
                    if (!envMap.containsKey(key)) {
                        envMap.put(key, value);
                    }
                });
            }
            if (!envMap.containsKey("ENV")) {
                envMap.put("ENV", DEFAULT_ENV);
            }
            if (!envMap.containsKey("ENVS")) {
                envMap.put("ENVS", DEFAULT_ENVS);
            }
            envMap.forEach((key, value) -> {
                envList.add(key.concat("=").concat(value));
            });

            dictConnMap.put(connName, envList);
        });
        this.setDictConnMap(envCmd(dictConnMap));
        this.setFlag(Boolean.TRUE);
        return this.getDictConnMap();
    }


    public Map<String, List<String>> envCmd(Map<String, List<String>> dictConnMap) {
        dictConnMap.keySet().forEach(connName -> {
            dictConnMap.put(connName, envCmd(dictConnMap.get(connName)));
        });
        return dictConnMap;
    }

    public List<String> envCmd(List<String> envList) {
        for (int i = 0; i < envList.size(); i++) {
            String evn = "echo -e " + envList.get(i);
            if (i == 0) {
                evn = evn + " >  /data/docker/.env";
            } else {
                evn = evn + " >>  /data/docker/.env";
            }
            envList.remove(i);
            envList.add(i, evn);
        }
        return envList;
    }
}
